//
//  MenuZhuanTiMainViewController.m
//  Products_B
//
//  Created by lanou on 16/6/4.
//  Copyright © 2016年 朋学友. All rights reserved.
//
// http://api.izhangchu.com/
 //methodName=TopicList&page=1&size=10&token=&type=1&user_id=&version=4.4

#import "MenuZhuanTiMainViewController.h"
#import "MenuZhuanTiMainModel.h"
#import "MenuZhuanTiMainTableViewCell.h"
#import "MenuZhuanTiDetailViewController.h"
@interface MenuZhuanTiMainViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)NSMutableArray *modelArray;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic ,strong)NSMutableDictionary *patDic;
@property (nonatomic, strong)NSString *page;
@end

@implementation MenuZhuanTiMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.modelArray = [NSMutableArray array];
    self.page = @"1";
    self.patDic = @{@"methodName":@"TopicList",@"page":@"1",@"size":@"10",@"token":@"",@"type":@"1",@"user_id":@"",@"&version":@"4.4"}.mutableCopy;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    [self.tableView registerClass:[MenuZhuanTiMainTableViewCell class] forCellReuseIdentifier:@"MenuZhuanTiMainTableViewCell"];
    self.tableView.rowHeight = kWidth * 418 / 1242 + 90;
    [self.view addSubview:_tableView];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.page = [NSString stringWithFormat:@"%ld",[self.page integerValue] + 1];
        self.patDic[@"page"] = self.page;
        [self requeatData];
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       self.page = @"1";
        self.patDic[@"page"] = self.page;
        [self requeatData];
    }];
    [self requeatData];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuZhuanTiMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MenuZhuanTiMainTableViewCell" forIndexPath:indexPath];
    MenuZhuanTiMainModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuZhuanTiDetailViewController *Vc = [[MenuZhuanTiDetailViewController alloc]init];
    
    MenuZhuanTiMainModel *model = self.modelArray[indexPath.row];
    Vc.shareID = model.id;
    [self.navigationController pushViewController:Vc animated:YES];
}
- (void)requeatData{
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:self.patDic finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if ([self.page isEqualToString:@"1"]) {
            [self.modelArray removeAllObjects];
        }

        NSArray *array = [MenuZhuanTiMainModel modelConfigureWithDic:jsonDic];
        for (MenuZhuanTiMainModel *model in array) {
            [self.modelArray addObject:model];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } error:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
